|
1
|
|
|
const ONE = 1; |
|
|
|
|
|
|
2
|
|
|
const HUNDRED = 100; |
|
|
|
|
|
|
3
|
|
|
const TEN_THOUSAND = 10000; |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* GeezCalculator calculate the ascii number from the parsed queue. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Sam As End <4sam21{at}gmail.com> |
|
9
|
|
|
*/ |
|
10
|
|
|
module.exports = class GeezCalculator { |
|
11
|
|
|
constructor($queue) { |
|
12
|
|
|
this.queue = $queue; |
|
13
|
|
|
this.total = 0; |
|
14
|
|
|
this.sub_total = 0; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Do the magic. |
|
19
|
|
|
*/ |
|
20
|
|
|
calculate() { |
|
21
|
|
|
this.resetSubTotalToZero(); |
|
22
|
|
|
|
|
23
|
|
|
this.queue.forEach(($token) => { |
|
24
|
|
|
this.processToken($token); |
|
25
|
|
|
}); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* set the sub total attribute to zero. |
|
30
|
|
|
*/ |
|
31
|
|
|
resetSubTotalToZero() { |
|
32
|
|
|
this.sub_total = 0; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Process a single token from the Queue. |
|
37
|
|
|
* |
|
38
|
|
|
* @param $token |
|
39
|
|
|
*/ |
|
40
|
|
|
processToken($token) { |
|
41
|
|
|
const $block = $token.block; |
|
42
|
|
|
const $separator = $token.separator; |
|
43
|
|
|
|
|
44
|
|
|
this.processBySeparator($block, $separator); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Process based on separator. |
|
49
|
|
|
* |
|
50
|
|
|
* @param $block |
|
51
|
|
|
* @param $separator |
|
52
|
|
|
*/ |
|
53
|
|
|
processBySeparator($block, $separator) { |
|
54
|
|
|
switch ($separator) { |
|
55
|
|
|
case ONE: |
|
|
|
|
|
|
56
|
|
|
this.addToTotal($block); |
|
57
|
|
|
break; |
|
58
|
|
|
case HUNDRED: |
|
|
|
|
|
|
59
|
|
|
this.updateSubTotal($block); |
|
60
|
|
|
break; |
|
61
|
|
|
case TEN_THOUSAND: |
|
|
|
|
|
|
62
|
|
|
default: |
|
63
|
|
|
this.updateTotal($block); |
|
64
|
|
|
break; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Add the sub total and the block to total |
|
70
|
|
|
* and reset sub total to zero. |
|
71
|
|
|
* |
|
72
|
|
|
* @param $block |
|
73
|
|
|
* |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
addToTotal($block) { |
|
77
|
|
|
this.total += this.sub_total + $block; |
|
78
|
|
|
this.resetSubTotalToZero(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Is the leading block? |
|
83
|
|
|
* |
|
84
|
|
|
* @param $block |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
isLeading($block) { |
|
89
|
|
|
return this.isBlockZero($block) && this.isSubtotalZero(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Is the value of block zero? |
|
94
|
|
|
* |
|
95
|
|
|
* @param $block |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
isBlockZero($block) { |
|
100
|
|
|
return this.isZero($block); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Is a number zero? |
|
105
|
|
|
* |
|
106
|
|
|
* @param $number |
|
107
|
|
|
* |
|
108
|
|
|
* @return boolean |
|
109
|
|
|
*/ |
|
110
|
|
|
isZero($number) { |
|
111
|
|
|
return $number === 0; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Is sub total attribute zero? |
|
116
|
|
|
* |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
|
isSubtotalZero() { |
|
120
|
|
|
return this.isZero(this.sub_total); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Add number to sun total. |
|
125
|
|
|
* |
|
126
|
|
|
* @param $number integer |
|
127
|
|
|
*/ |
|
128
|
|
|
addToSubTotal($number) { |
|
129
|
|
|
this.sub_total += $number; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Is the leading 10k? |
|
134
|
|
|
* |
|
135
|
|
|
* @param $block |
|
136
|
|
|
* |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
|
|
isLeadingTenThousand($block) { |
|
140
|
|
|
return this.isTotalZero() && this.isLeading($block); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Is the total attribute zero? |
|
145
|
|
|
* |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
isTotalZero() { |
|
149
|
|
|
return this.isZero(this.total); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Multiply the total attribute by ten thousand. |
|
154
|
|
|
*/ |
|
155
|
|
|
multiplyTotalBy10k() { |
|
156
|
|
|
this.total *= TEN_THOUSAND; |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Return the calculated ascii number. |
|
161
|
|
|
* |
|
162
|
|
|
* @return int |
|
163
|
|
|
*/ |
|
164
|
|
|
getCalculated() { |
|
165
|
|
|
return this.total; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Update the sub total attribute. |
|
170
|
|
|
* |
|
171
|
|
|
* @param $block |
|
172
|
|
|
*/ |
|
173
|
|
|
updateSubTotal($block) { |
|
174
|
|
|
/* eslint-disable no-param-reassign */ |
|
175
|
|
|
if (this.isLeading($block)) { |
|
176
|
|
|
$block = ONE; |
|
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$block *= HUNDRED; |
|
|
|
|
|
|
180
|
|
|
/* eslint-enable no-param-reassign */ |
|
181
|
|
|
|
|
182
|
|
|
this.addToSubTotal($block); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Update the sub total attribute. |
|
187
|
|
|
* |
|
188
|
|
|
* @param $block |
|
189
|
|
|
*/ |
|
190
|
|
|
updateTotal($block) { |
|
191
|
|
|
if (this.isLeadingTenThousand($block)) { |
|
192
|
|
|
// eslint-disable-next-line no-param-reassign |
|
193
|
|
|
$block = ONE; |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
this.addToTotal($block); |
|
197
|
|
|
this.multiplyTotalBy10k(); |
|
198
|
|
|
} |
|
199
|
|
|
}; |
|
200
|
|
|
|